import type {Router} from "vue-router";
import {createRouter, createWebHashHistory} from "vue-router";
import menuStore from "@/apps/admin/store/menu"
import store from "@/store/index"
import guardStore from "@/apps/admin/store/guard"
import appStore from "@/store/app"

export default class AdminRouter {
    /**
     * 默认注册的路由
     */
    DefaultRoutes: Array<any>;


    //@ts-ignore 路由实例
    AppRouter: Router

    //页面实例数据
    Pages: { [key: string]: any } = {}


    //@ts-ignore 构造函数
    constructor(DefaultRoutes: Array<any>, pages: { [key: string]: any }) {
        this.DefaultRoutes = DefaultRoutes //默认理由
        this.Pages = pages
        this.createRouter()
    }

    /**
     * 创建路由信息
     */
    createRouter() {
        if (this.AppRouter) {
            return this.AppRouter
        }

        /**
         * 创建路由
         */
        this.AppRouter = createRouter({
            history: createWebHashHistory('/'),//路由模式
            //@ts-ignore不加就要警告，这是真他喵的多余又烦人,逼死强迫症
            routes: this.DefaultRoutes,
        })


        /**
         * 路由前置处理器
         */
        this.AppRouter.beforeEach((to, form, next) => {
            this.beforeEach(to, form, next)
        })


        /**
         * 后置酷游
         */
        this.AppRouter.afterEach((to, from, failure) => {
            if (typeof to?.meta?.title === 'string') {
                document.title = to.meta.title ? to.meta.title : 'arco admin'
            }
        })

        return this.AppRouter
    }


    //@ts-ignore 理由前置拦截器
    private beforeEach(to, form, next) {
        const mstore = menuStore(store)//菜单状态
        const astore = appStore(store)//项目配置
        if (to.path === '/') {//授权页默认调到真的首页
            return next({path:astore.desktop, replace: true})
        }
        const source = to?.meta?.local
        if (typeof source !== 'boolean') {//非法来源的URL
            return next({path: '/not-found', replace: true})//注册完成,往下进入路由页面
        } else {
            const gstore = guardStore(store)//用户状态
            if (gstore.tourist) {//未登录
                if (to.path === '/login/index') {//当前在登录页面
                    return next()//已经在登录页面
                }
                return next({path: "/login/index", replace: true})
            }
            if (to.path === '/login/index') {//当前在登录页
                return next({path:astore.desktop, replace: true})
            }
            if (source) {//本地路由
                return next()//已经在登录页面
            } else if (typeof to?.meta?.title === 'undefined') {//远程路由,且还未重载
                const info: any = mstore.getRouterInfo(to.path)
                if (typeof info !== 'object') {//没有定义这个路由
                    return next({path: '/not-found', replace: true})//跳转到404
                }
                this.AppRouter.removeRoute(to.name)//删除路由
                this.AppRouter.addRoute('admin-layout', {//重载路由
                    ...info, meta: {local: source, ...info.meta}, component: this.Pages[info.path]
                })
                return next({path: info.path, replace: true,params:to.params,query:to.query})
            }
            return next()
        }
    }
}
